﻿using System;
using System.ComponentModel.DataAnnotations;

namespace IssueVision.EntityModel
{
    /// <summary>
    /// User class client-side extensions
    /// </summary>
    public partial class User
    {
        /// <summary>
        /// Returns each user in the format of "user (FirstName LastName)"
        /// </summary>
        public string DisplayName
        {
            get { return _name == null ? " " : _name + " (" + FullName.FirstName + " " + FullName.LastName + ")"; }
        }

        /// <summary>
        /// Returns each user type in the format of "Admin" or "User"
        /// </summary>
        public string DisplayUserType
        {
            get
            {
                switch (Settings.UserType)
                {
                    case "A":
                        return IssueVisionServiceConstant.UserTypeAdmin;
                    case "U":
                        return IssueVisionServiceConstant.UserTypeUser;
                    default:
                        return string.Empty;
                }
            }
            set
            {
                if (value != null)
                {
                    if (value.Contains(IssueVisionServiceConstant.UserTypeAdmin))
                    {
                        // Admin User
                        Settings.UserType = "A";
                    }
                    else if (value.Contains(IssueVisionServiceConstant.UserTypeUser))
                    {
                        // Normal User
                        Settings.UserType = "U";
                    }
                    else
                        Settings.UserType = String.Empty;
                }
                else
                    Settings.UserType = String.Empty;
            }
        }

        /// <summary>
        /// Stores the new password the user entered in the MyProfile UI,
        /// even if it is invalid.  This way we can validate the new password
        /// confirmation adequately all the times
        /// </summary>
        public string ActualNewPassword { get; set; }

        [Display(Name = "Confirm new password")]
        [Required(ErrorMessage = "This field is required.")]
        [CustomValidation(typeof(User), "CheckNewPasswordConfirmation")]
        public string NewPasswordConfirmation
        {
            get
            {
                return _newPasswordConfirmation;
            }

            set
            {
                PropertySetterEntry("NewPasswordConfirmation");
                _newPasswordConfirmation = value;
                PropertySetterExit("NewPasswordConfirmation", value);
                OnPropertyChanged("NewPasswordConfirmation");
            }
        }
        private string _newPasswordConfirmation;

        /// <summary>
        /// Stores the password answer the user entered in the MyProfile UI,
        /// even if it is invalid.  This way we can validate the password answer
        /// confirmation adequately all the times
        /// </summary>
        public string ActualPasswordAnswer { get; set; }

        [Display(Name = "Confirm security answer")]
        [Required(ErrorMessage = "This field is required.")]
        [CustomValidation(typeof(User), "CheckPasswordAnswerConfirmation")]
        public string PasswordAnswerConfirmation
        {
            get
            {
                return _passwordAnswerConfirmation;
            }

            set
            {
                PropertySetterEntry("PasswordAnswerConfirmation");
                _passwordAnswerConfirmation = value;
                PropertySetterExit("PasswordAnswerConfirmation", value);
                OnPropertyChanged("PasswordAnswerConfirmation");
            }
        }
        private string _passwordAnswerConfirmation;

        /// <summary>
        /// Custom validation of whether new password and confirm password match
        /// </summary>
        /// <param name="newPasswordConfirmation"></param>
        /// <param name="validationContext"></param>
        /// <returns></returns>
        public static ValidationResult CheckNewPasswordConfirmation(string newPasswordConfirmation, ValidationContext validationContext)
        {
            User currentUser = (User)validationContext.ObjectInstance;

            if (!string.IsNullOrEmpty(currentUser.ActualNewPassword) &&
                !string.IsNullOrEmpty(newPasswordConfirmation) &&
                currentUser.ActualNewPassword != newPasswordConfirmation)
            {
                return new ValidationResult("Passwords do not match.", new[] { "NewPasswordConfirmation" });
            }

            return ValidationResult.Success;
        }

        /// <summary>
        /// Custom validation of whether password answer and confirm password answer match
        /// </summary>
        /// <param name="passwordAnswerConfirmation"></param>
        /// <param name="validationContext"></param>
        /// <returns></returns>
        public static ValidationResult CheckPasswordAnswerConfirmation(string passwordAnswerConfirmation, ValidationContext validationContext)
        {
            User currentUser = (User)validationContext.ObjectInstance;

            if (!string.IsNullOrEmpty(currentUser.ActualPasswordAnswer) &&
                !string.IsNullOrEmpty(passwordAnswerConfirmation) &&
                currentUser.ActualPasswordAnswer != passwordAnswerConfirmation)
            {
                return new ValidationResult("Security answers do not match", new[] { "PasswordAnswerConfirmation" });
            }

            return ValidationResult.Success;
        }
    }
}
